package cn.iocoder.yudao.module.member.service.useranswerremark;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.useranswerremark.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.useranswerremark.UserAnswerRemarkDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.useranswerremark.UserAnswerRemarkMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户问答评论 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserAnswerRemarkServiceImpl implements UserAnswerRemarkService {

    @Resource
    private UserAnswerRemarkMapper userAnswerRemarkMapper;

    @Override
    public Long createUserAnswerRemark(UserAnswerRemarkSaveReqVO createReqVO) {
        // 插入
        UserAnswerRemarkDO userAnswerRemark = BeanUtils.toBean(createReqVO, UserAnswerRemarkDO.class);
        userAnswerRemarkMapper.insert(userAnswerRemark);
        // 返回
        return userAnswerRemark.getId();
    }

    @Override
    public void updateUserAnswerRemark(UserAnswerRemarkSaveReqVO updateReqVO) {
        // 校验存在
        validateUserAnswerRemarkExists(updateReqVO.getId());
        // 更新
        UserAnswerRemarkDO updateObj = BeanUtils.toBean(updateReqVO, UserAnswerRemarkDO.class);
        userAnswerRemarkMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserAnswerRemark(Long id) {
        // 校验存在
        validateUserAnswerRemarkExists(id);
        // 删除
        userAnswerRemarkMapper.deleteById(id);
    }

    private void validateUserAnswerRemarkExists(Long id) {
        if (userAnswerRemarkMapper.selectById(id) == null) {
            throw exception(USER_ANSWER_REMARK_NOT_EXISTS);
        }
    }

    @Override
    public UserAnswerRemarkDO getUserAnswerRemark(Long id) {
        return userAnswerRemarkMapper.selectById(id);
    }

    @Override
    public PageResult<UserAnswerRemarkDO> getUserAnswerRemarkPage(UserAnswerRemarkPageReqVO pageReqVO) {
        return userAnswerRemarkMapper.selectPage(pageReqVO);
    }

}